﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Windows8Accelerator.Core.Xaml.Controls;

namespace Windows8Accelerator.Core.Xaml.Helpers
{
    public class WebHelper : WebHelperBase
    {
        protected override async Task<string> InternalGetStringAsync(Uri uri, string accept = null, string user = null, string password = null)
        {
            ICredentials credentials = CredentialCache.DefaultNetworkCredentials;
            if (user != null)
            {
                credentials = new NetworkCredential(user, password);
            }
            using (var _handler = new HttpClientHandler
            {
                Credentials = credentials,
            })
            {
                using (var _client = new HttpClient(_handler))
                {
                    if (accept != null)
                        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept));

                    using (var response = await _client.GetAsync(uri))
                    {
                        response.EnsureSuccessStatusCode();
                        return await response.Content.ReadAsStringAsync();
                    }
                }
            }
        }

        protected async override Task<string> InternalPostStringAsync(Uri uri, object postData, string accept, string user, string password)
        {
            ICredentials credentials = CredentialCache.DefaultNetworkCredentials;
            if (user != null)
            {
                credentials = new NetworkCredential(user, password);
            }
            using (var _handler = new HttpClientHandler
            {
                Credentials = credentials,
            })
            {
                using (var _client = new HttpClient(_handler))
                {
                    if (accept != null)
                        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept));

                    HttpContent httpContent;
                    if (postData is IEnumerable<KeyValuePair<string, string>>)
                        httpContent = new FormUrlEncodedContent(postData as IEnumerable<KeyValuePair<string, string>>);
                    else
                    {
                        httpContent = new StringContent(postData.ToString(), Encoding.UTF8, "application/json");
                        httpContent.Headers.Add("datatype", "jsondata");
                        httpContent.Headers.Add("async", "false");
                    }

                    using (var response = await _client.PostAsync(uri, httpContent))
                    {
                        response.EnsureSuccessStatusCode();
                        return await response.Content.ReadAsStringAsync();
                    }
                }
            }
        }

        public override async Task<Stream> GetStreamAsync(Uri uri)
        {
            ICredentials credentials = CredentialCache.DefaultNetworkCredentials;
            using (var _handler = new HttpClientHandler
            {
                Credentials = credentials,
            })
            {
                using (var _client = new HttpClient(_handler))
                {
                    using (var response = await _client.GetAsync(uri))
                    {
                        response.EnsureSuccessStatusCode();
                        return await response.Content.ReadAsStreamAsync();
                    }
                }
            }
        }
    }
}
